home *** CD-ROM | disk | FTP | other *** search
/ Aminet 43 / Aminet 43 (2001)(GTI - Schatztruhe)[!][Jun 2001].iso / Aminet / game / think / Connect4.lha / Connect4 / source / computer_player.c next >
C/C++ Source or Header  |  2001-03-21  |  2KB  |  69 lines

  1. #include "computer_player.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <resources/battclock.h>
  5. #include <clib/exec_protos.h>
  6. #include <clib/battclock_protos.h>
  7. /*
  8. **  Inputs:
  9. **          skill_level     1 being the lowest skill level
  10. **                          1 being the highest skill level
  11. **          filled          2D array describing gameboard contents
  12. **          columntop
  13. **
  14. **  Result:
  15. **          Computer player's choice of column in which to place piece
  16. **          -1 on error
  17. */
  18. APTR BattClockBase=NULL;
  19.  
  20. int computer_player (int skill_level, char **filled, char *columntop, BOOL newgame)
  21. {
  22.     enum {ERROR=-1};
  23.  
  24.     if (BattClockBase=OpenResource(BATTCLOCKNAME))
  25.     {
  26.         if (newgame)
  27.             srand(ReadBattClock()); // Seed the random generator with the system clock
  28.                                     // to ensure the moves are different each time
  29.         switch (skill_level)
  30.         {
  31.             // just needs columntop
  32.             case 1:
  33.             {
  34.                 int result;
  35.                 BOOL badmove=TRUE;
  36.  
  37.                 while (badmove)
  38.                 {
  39.                     result=rand();
  40.                     result-=(result/7)*7;
  41.                     if (columntop[result]!=4)
  42.                         badmove=FALSE;
  43.                 }
  44.  
  45.                 return result;
  46.             }
  47.  
  48.             default:
  49.                 return ERROR;
  50.         }
  51.     }
  52.  
  53.     return ERROR;
  54. }
  55. /*
  56. int main (void)
  57. {
  58.     int i;
  59.  
  60.     printf("%d ", computer_player(1, NULL, NULL, TRUE));
  61.     for (i=0; i<19; i++)
  62.         printf("%d ", computer_player(1, NULL, NULL, FALSE));
  63.  
  64.     puts("");
  65.  
  66.     return 0;
  67. }
  68. */
  69.